package com.naiterui.ehp.bs.ds.modules.sys.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bs.ds.modules.sys.entity.RoleDept;
import com.naiterui.ehp.bs.ds.modules.sys.mapper.RoleDeptMapper;
import com.naiterui.ehp.bs.ds.modules.sys.service.IRoleDeptService;

/**
 * 角色与部门对应关系
 */
@Service
public class RoleDeptServiceImpl extends ServiceImpl<RoleDeptMapper, RoleDept> implements IRoleDeptService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> deptIdList) {
        // 先删除角色与部门关系
        this.deleteBatch(new Long[]{roleId});
        if (deptIdList.isEmpty()) {
            return;
        }
        // 保存角色与菜单关系
        this.saveBatch(deptIdList.stream().map(deptId -> new RoleDept(roleId, deptId)).collect(Collectors.toList()));
    }

    @Override
    public List<Long> queryDeptIdList(Long[] roleIds) {
        return this.baseMapper.queryDeptIdList(roleIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        this.baseMapper.deleteBatch(roleIds);
    }
}
